package com.ruoyi.lost.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.lost.bo.HyLostFoundQueryBo;
import com.ruoyi.lost.domain.HyLostFound;
import com.ruoyi.lost.mapper.HyLostFoundMapper;
import com.ruoyi.lost.service.IHyLostFoundService;
import com.ruoyi.system.domain.HyUser;
import com.ruoyi.system.service.IHyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 失物招领管理Service业务层处理
 *
 * @author lixin
 * @date 2021-08-05
 */
@Service
public class HyLostFoundServiceImpl extends ServicePlusImpl<HyLostFoundMapper, HyLostFound> implements IHyLostFoundService {

	@Autowired
	private IHyUserService hyUserService;

    @Override
    public HyLostFound queryById(Long foundId){
        return getById(foundId);
    }

    @Override
    public TableDataInfo<HyLostFound> queryPageList(HyLostFoundQueryBo bo) {
        return PageUtils.buildDataInfo(page(PageUtils.buildPage(), buildQueryWrapper(bo)));
    }

    @Override
    public List<HyLostFound> queryList(HyLostFoundQueryBo bo) {
		LambdaQueryWrapper<HyLostFound> lqw = buildQueryWrapper(bo);
		lqw.orderByDesc(HyLostFound::getCreateTime);

		return list(lqw);
    }

    private LambdaQueryWrapper<HyLostFound> buildQueryWrapper(HyLostFoundQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HyLostFound> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HyLostFound::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), HyLostFound::getTitle, bo.getTitle());
        lqw.eq(bo.getCreateTime() != null, HyLostFound::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getStatus() != null, HyLostFound::getStatus, bo.getStatus());
        lqw.eq(bo.getType() != null, HyLostFound::getType, bo.getType());
		lqw.eq(bo.getHandleUserId() != null, HyLostFound::getHandleUserId, bo.getHandleUserId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HyLostFound bo) {
        validEntityBeforeSave(bo);
        return save(bo);
    }

    @Override
    public Boolean updateByEditBo(HyLostFound bo) {
        validEntityBeforeSave(bo);
        return updateById(bo);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HyLostFound entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	/**
	 * 失物招领发布
	 * @param bo
	 * @return
	 */
	@Override
	public Long publish(HyLostFound bo) {

		validEntityBeforeSave(bo);
		this.saveOrUpdate(bo);
		return bo.getFoundId();
	}

	@Override
	public HyUser queryUserByFoundId(Long foundId) {
		HyLostFound lostFound = baseMapper.selectById(foundId);
		HyUser user = hyUserService.getOne(new QueryWrapper<HyUser>()
			.eq("user_id", lostFound.getUserId())
			.select("user_id", "nick_name", "email", "qqnumber", "sex", "avatar", "remark"));
		return user;
	}

	/**
	 * 删除失物招领
	 * @param foundId
	 * @return
	 */
	@Override
	public boolean deleteByFoundId(Long foundId) {
		return this.removeById(foundId);
	}

	/**
	 * 物品认领
	 * @param user 认领用户
	 * @param foundId 帖子ID
	 * @return
	 */
	@Override
	public boolean claim(HyUser user, Long foundId) {
		HyLostFound lostFound = this.getById(foundId);
		if(ObjectUtil.isNull(lostFound)) {
			throw new CustomException("认领不存在");
		}
		if(lostFound.getStatus() != 0) {
			throw new CustomException("该物品已被人认领");
		}
		lostFound.setHandleUserId(user.getUserId());
		lostFound.setHandleTime(new Date());
		lostFound.setStatus(2);
		return this.updateById(lostFound);
	}

	/**
	 * 物品归还
	 * @param user 归还用户
	 * @param foundId 帖子ID
	 * @return
	 */
	@Override
	public boolean returnFound(HyUser user, Long foundId) {
		HyLostFound lostFound = this.getById(foundId);
		if(ObjectUtil.isNull(lostFound)) {
			throw new CustomException("寻物不存在");
		}
		if(lostFound.getStatus() != 0) {
			throw new CustomException("该物品已被人归还");
		}
		lostFound.setHandleUserId(user.getUserId());
		lostFound.setHandleTime(new Date());
		lostFound.setStatus(1);
		return this.updateById(lostFound);
	}

	/**
	 * 查询我的认领
	 * @param userId
	 * @return
	 */
	@Override
	public List<HyLostFound> queryListByClaimUserId(Long userId) {
		HyLostFoundQueryBo bo = new HyLostFoundQueryBo();
		bo.setType(1);
		bo.setHandleUserId(userId);
		LambdaQueryWrapper<HyLostFound> lqw = buildQueryWrapper(bo);
		lqw.orderByDesc(HyLostFound::getHandleTime);
		return list(lqw);
	}

	/**
	 * 查询我的归还
	 * @param userId
	 * @return
	 */
	@Override
	public List<HyLostFound> queryListByReturnUserId(Long userId) {
		HyLostFoundQueryBo bo = new HyLostFoundQueryBo();
		bo.setType(2);
		bo.setHandleUserId(userId);
		LambdaQueryWrapper<HyLostFound> lqw = buildQueryWrapper(bo);
		lqw.orderByDesc(HyLostFound::getHandleTime);
		return list(lqw);
	}

	/**
	 * 我发布的-已解决
	 * @param bo
	 * @return
	 */
	@Override
	public List<HyLostFound> queryResolvedList(HyLostFoundQueryBo bo) {
		LambdaQueryWrapper<HyLostFound> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getUserId() != null, HyLostFound::getUserId, bo.getUserId());
		lqw.ne(HyLostFound::getStatus, "0");
		return list(lqw);
	}


}
